package com.china08.yxyapi.api.manage.tec.office.website.article;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.sws.SwsSection;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.SwsSectionRepository;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class SwsTeaArticleSectionListController extends YxyApiBaseController {

    @Autowired
    SwsSectionRepository swsSectionRepository;

    @ApiOperation("获取栏目")
    @RequestMapping(value = { "/api_yw/manage/teaching/article/section/list",
            "/api_yw/manage/tec/office/website/article/section/list" }, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {
        String pid = reqModel.pid;
        String schoolId = getProfile(request, response).getSchoolId();

        List<RespModel> returnList = new ArrayList<>();

        // 内容型栏目
        List<SwsSection> swsSectionList = swsSectionRepository
                .findBySchoolIdAndPidAndSectionTypeOrderByOrderNumAsc(schoolId, pid, 0);

        for (SwsSection swsSection : swsSectionList) {
            RespModel respModel = new RespModel();
            respModel.id = swsSection.getId();
            respModel.sectionName = swsSection.getSectionName();
            returnList.add(respModel);
        }
        return returnList;
    }

    protected static class ReqModel extends YxyApiPageReqModel {
        public String pid;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(pid), "栏目级别为空");
        }
    }

    protected static class RespModel extends YxyApiRespModel {
        public String id;
        public String sectionName;
    }
}
